home *** CD-ROM | disk | FTP | other *** search
- Path: lince.lander.es!news
- From: Leonardo <leonardo@lander.es>
- Newsgroups: comp.lang.c++
- Subject: Re: Why C++ sucks++
- Date: 18 Feb 1996 11:45:23 GMT
- Organization: Bla bla bla
- Message-ID: <4g73gj$6cc@lince.lander.es>
- References: <1996Feb17.181435.1@willow>
- NNTP-Posting-Host: ppp032.lander.es
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
-
- ferriom@woods.uml.edu wrote:
- >
- >
- >*** HIGH SCHOOL ***
- >
- >100 PRINT "Hello World."
- >999 END
- >
- >
- >
- >*** COLLEGE FRESHMAN ***
- >
- > WRITE (6, 100)
- > 100 FORMAT (1X, 'Hello World.')
- > END
- >
- >
- >
- >*** ADVANCED COLLEGE ***
- >
- >PROGRAM P1(INPUT, OUTPUT);
- >
- >BEGIN
- >WRITELN(OUTPUT, 'Hello World.');
- >END.
- >
- >
- >
- >*** RECENT COLLEGE GRADUATE ***
- >
- > IDENTIFICATION DIVISION.
- > PROGRAM-ID.
- > P1.
- >
- > ENVIRONMENT DIVISION.
- >
- > DATA DIVISION.
- > WORKING-STORAGE SECTION.
- > 01 MESSAGE-RECORD PICTURE X(12).
- >
- > PROCEDURE DIVISION.
- > PROGRAM-BEGIN.
- > FIRST-PARAGRAPH.
- > MOVE 'Hello World.' TO MESSAGE-RECORD.
- > DISPLAY MESSAGE-RECORD.
- > PROGRAM-DONE.
- > STOP RUN.
- >
- >
- >
- >*** SEASONED PROFESSIONAL ***
- >
- >#include <iostream.h>
- >#include <string.h>
- >
- >
- >class String
- >{
- > public:
- > String();
- > String(const char *const);
- > String(const String &);
- > ~String();
- > char & operator[](unsigned short offset);
- > char operator[](unsigned short offset) const;
- > String operator+(const String&);
- > void operator+=(const String&);
- > String & operator= (const String &);
- > unsigned short GetLen()const {return itsLen; }
- > const char * GetString() const {return itsString; }
- > private:
- > String (unsigned short);
- > char * itsString;
- > unsigned short itsLen;
- >};
- >
- >
- >String::String()
- >{
- > itsString = new char[1];
- > itsString[0] = '\0';
- > itsLen = 0;
- >}
- >
- >String::String(unsigned short len)
- >{
- > itsString = new char[len + 1];
- > for (unsigned short i = 0; i <= len; i++)
- > itsString[i] = '\0';
- > itsLen = len;
- >}
- >
- >String::String(const char * const cString)
- >{
- > itsLen = strlen(cString);
- > itsString = new char[itsLen + 1];
- > for (unsigned short i = 0; i < itsLen; i++)
- > itsString[i] = cString[i];
- > itsString[itsLen] = '\0';
- >}
- >
- >String::String(const String & rhs)
- >{
- > itsLen = rhs.GetLen();
- > itsString = new char[itsLen + 1];
- > for (unsigned short i = 0; i < itsLen; i++)
- > itsString[i] = rhs[i];
- > itsString[itsLen] = '\0';
- >}
- >
- >String::~String()
- >{
- > delete [] itsString;
- > itsLen = 0;
- >}
- >
- >String& String::operator=(const String & rhs)
- >{
- > if (this == &rhs)
- > return *this;
- > delete [] itsString;
- > itsLen = rhs.GetLen();
- > itsString = new char[itsLen + 1];
- > for (unsigned short i = 0; i < itsLen; i++)
- > itsString[i] = rhs[i];
- > itsString[itsLen] = '\0';
- > return *this;
- >}
- >
- >char & String::operator[](unsigned short offset)
- >{
- > if (offset > itsLen)
- > return itsString[itsLen - 1];
- > else
- > return itsString[offset];
- >}
- >
- >char String::operator[](unsigned short offset) const
- >{
- > if (offset > itsLen)
- > return itsString[itsLen - 1];
- > else
- > return itsString[offset];
- >}
- >
- >String String::operator+(const String& rhs)
- >{
- > unsigned short totalLen = itsLen + rhs.GetLen();
- > String temp(totalLen);
- > for (unsigned short i = 0; i < itsLen; i++)
- > temp[i] = itsString[i];
- > for (unsigned short j = 0; j < rhs.GetLen(); j++, i++)
- > temp[i] = rhs[j];
- > temp[totalLen] = '\0';
- > return temp;
- >}
- >
- >void String::operator+=(const String& rhs)
- >{
- > unsigned short rhsLen = rhs.GetLen();
- > unsigned short totalLen = itsLen + rhsLen;
- > String temp(totalLen);
- > for (unsigned short i = 0; i < itsLen; i++)
- > temp[i] = itsString[i];
- > for (unsigned short j = 0; j < rhs.GetLen(); j++, i++)
- > temp[i] = rhs[i - itsLen];
- > temp[totalLen] = '\0';
- > *this = temp;
- >}
- >
- >
- >main()
- >{
- > String s1("Hello");
- > String s2(" ");
- > String s3("World.");
- > String s4 = s1 + s2 + s3;
- > cout << s4.GetString() << endl;
- >}
-
- Hey pal, I don't know why I even bother.
- The code comparable to that written in all other languages would be
- something like this:
-
- #include <iostream.h>
-
- main()
- {
- cout << "Hello fucken world" << endl;
- }
-
- And the method is much more type-safe not only for strings but for all
- other types that you create.
- Bye
-
-
-